package cn.cttic.base.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.base.api.domain.vo.BaseDeviceVo;
import cn.cttic.base.api.domain.bo.BaseDeviceBo;
import cn.cttic.base.service.IBaseDeviceService;
import cn.cttic.common.mybatis.core.page.TableDataInfo;

/**
 * 设备型号
 * 前端访问路由地址为:/base/device
 *
 * @author TImes.Studio
 * @date 2024-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device")
public class BaseDeviceController extends BaseController {

    private final IBaseDeviceService baseDeviceService;

    /**
     * 查询设备型号列表
     */
    @SaCheckPermission("base:device:list")
    @GetMapping("/list")
    public TableDataInfo<BaseDeviceVo> list(BaseDeviceBo bo, PageQuery pageQuery) {
        return baseDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备型号列表
     */
    @SaCheckPermission("base:device:export")
    @Log(title = "设备型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseDeviceBo bo, HttpServletResponse response) {
        List<BaseDeviceVo> list = baseDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备型号", BaseDeviceVo.class, response);
    }

    /**
     * 获取设备型号详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:device:query")
    @GetMapping("/{id}")
    public R<BaseDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(baseDeviceService.queryById(id));
    }

    /**
     * 新增设备型号
     */
    @SaCheckPermission("base:device:add")
    @Log(title = "设备型号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseDeviceBo bo) {
        return toAjax(baseDeviceService.insertByBo(bo));
    }

    /**
     * 修改设备型号
     */
    @SaCheckPermission("base:device:edit")
    @Log(title = "设备型号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseDeviceBo bo) {
        return toAjax(baseDeviceService.updateByBo(bo));
    }

    /**
     * 删除设备型号
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:device:remove")
    @Log(title = "设备型号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(baseDeviceService.deleteWithValidByIds(List.of(ids), true));
    }
}
